package com.example.demoddd.domain.userauth.services;


import com.example.demoddd.application.dto.UserDto;
import com.example.demoddd.domain.userauth.model.User;
import com.example.demoddd.domain.userauth.model.entity.StUser;
import com.example.demoddd.domain.userauth.model.entity.table.StUserTableDef;
import com.example.demoddd.domain.userauth.repositorise.StUserMapper;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 用户领域服务
 */
@Service
@RequiredArgsConstructor
public class UserService extends ServiceImpl<StUserMapper, StUser> {
    private final PermissionService permissionService;

    public User registerUser(String username, String password) {
        // 用户注册逻辑
        return new User();
    }

    public UserDto createUser(UserDto.CreateUserDto createUserDto) {
        User user = null;

        return UserDto.build(user);
    }

    /**
     * @param unique 唯一性的字段 入参和 登录时UsernamePasswordAuthenticationToken 保持 一致
     * @return
     */
    public User findByUsername(String unique) {
        QueryWrapper queryWrapper = QueryWrapper.create();
        StUser stUser = this.getOne(queryWrapper.and(StUserTableDef.ST_USER.EMAIL.eq(unique)));

        if (null == stUser) {
            return null;
        }

        User build = User.build(stUser);
        ////查权限
        List<String> permissions = permissionService.queryPermissionsByUserid(stUser.getUserId());
        build.setPermissions(permissions);
        return build;
    }

}
